import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home/Home.vue'
import Search from '../views/Search.vue'
import List from '../views/List/List.vue'
import Cart from '../views/Cart/Cart.vue'
import Order from '../views/Order/Order.vue'
import Mine from '../views/Mine/Mine.vue'
import Pay from '../views/Pay/Pay.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Address from '../views/Address/Address.vue'
import Goods from '../views/Goods/Goods.vue'
import { useStore } from 'vuex'

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home,
    },
    {
        path: '/search',
        name: 'search',
        component: Search,
    },
    {
        path: '/list',
        name: 'list',
        component: List,
    },
    {
        path: '/cart',
        name: 'cart',
        component: Cart,
    },
    {
        path: '/mine',
        name: 'mine',
        component: Mine,
    },
    {
        path: '/order',
        name: 'order',
        component: Order
    },
    {
        path: '/pay',
        name: 'pay',
        component: Pay,
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/register',
        name: 'register',
        component: Register
    },
    {
        path: '/address',
        name: 'address',
        component: Address,
        beforeEnter:(to,from,next)=>{
            if (!sessionStorage.getItem('isLogin')) {
                // 说明未登录，跳去路由页面
                router.push('/login?redirct='+to.fullPath)
            }else{
            // 已登录，正常跳转
                next()
            }
        }
    },
    {
        path: "/chanAddress",
        name: "ChanAddress",
        // route level code-splitting
        // this generates a separate chunk (chanAddress.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: function () {
          return import(/* webpackChunkName: "chanAddress" */ "../views/SetAddress/ChanAddress.vue");
        },
      },
    {
        path: '/goods',
        name: 'goods',
        component: Goods
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

export default router
